package cn.kgc.springboot.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author: hengge
 * @version: v1.0
 * @DATE: 2022/11/10
 */
@Component
public class RedisUtil {

    @Autowired(required = false)
    private RedisTemplate<String,String> redisTemplate;

    //1.设置string类型的数据
//    public void setValue(String key,String value){
//        redisTemplate.opsForValue().set(key, value);
//    }

    //2.添加string类型的值，同时设置存活时间
    public void setValue(String key, String value, Long timeout, TimeUnit unit){
        redisTemplate.opsForValue().set(key, value, timeout, unit);
    }

    //3.获取指定key的值
    public String getValue(String key){
        return redisTemplate.opsForValue().get(key);
    }

    //4.设置值之前,先判定key是否存在
//    public void setIfAbsent(String key,String value){
//        redisTemplate.opsForValue().setIfAbsent(key, value);
//    }

    //5.修改
//    public void setIfPresent(String key,String value){
//        redisTemplate.opsForValue().setIfPresent(key, value);
//    }

    //6.删除指定key
//    public Boolean deleteByKey(String key){
//        return redisTemplate.delete(key);
//    }

    //7.是否包含指定key
//    public Boolean hasKey(String key){
//        return redisTemplate.hasKey(key);
//    }
}
